package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.business.domain.MobileUser;
import com.ruoyi.business.domain.MobileUserCoupon;
import com.ruoyi.business.mapper.MobileUserCouponMapper;
import com.ruoyi.business.mapper.MobileUserMapper;
import com.ruoyi.business.service.IMobileUserCouponService;
import com.ruoyi.common.exception.MessageException;
import com.ruoyi.common.utils.QRCodeUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 我的优惠券Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-14
 */
@Service
public class MobileUserCouponServiceImpl extends ServiceImpl<MobileUserCouponMapper, MobileUserCoupon> implements IMobileUserCouponService {

    @Autowired
    private MobileUserCouponMapper couponMapper;
    @Autowired
    private MobileUserMapper userMapper;

    /**
     * 查询我的优惠券列表
     *
     * @param mobileUserCoupon 我的优惠券
     * @return 我的优惠券
     */
    @Override
    public List<MobileUserCoupon> selectMobileUserCouponList(MobileUserCoupon mobileUserCoupon) {
        return couponMapper.selectMobileUserCouponList(mobileUserCoupon);
    }

    /**
     * @param openId
     * @return void
     * @Author liumaotong
     * @Description 领取优惠券
     * @Date 2024/6/14
     * @Param [openId]
     */
    @Override
    public void getCoupon(String openId) {
        if(StringUtils.isBlank(openId)){
            throw new MessageException("openId不能为空");
        }
        //查询用户信息
        QueryWrapper<MobileUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(MobileUser::getOpenId,openId);
        MobileUser user = userMapper.selectOne(userQueryWrapper);

        //查询优惠券数据
        QueryWrapper<MobileUserCoupon> couponQueryWrapper = new QueryWrapper<>();
        couponQueryWrapper.lambda().eq(MobileUserCoupon::getUserId,user.getId());
        MobileUserCoupon coupon = couponMapper.selectOne(couponQueryWrapper);
        if(null != coupon){
            throw new MessageException("您已领取优惠券，无法再次领取");
        }
        //保存优惠券数据
        MobileUserCoupon userCoupon = new MobileUserCoupon();
        userCoupon.setUserId(user.getId());
        userCoupon.setCouponQrCode(IdUtils.fastSimpleUUID());
        userCoupon.setStatus("0");
        userCoupon.setCreateBy(String.valueOf(user.getId()));
        userCoupon.setCreateTime(new Date());
        userCoupon.setUpdateBy(String.valueOf(user.getId()));
        userCoupon.setUpdateTime(new Date());
        couponMapper.insert(userCoupon);
    }

    /**
     * @param openId
     * @return com.ruoyi.business.domain.MobileUserCoupon
     * @Author liumaotong
     * @Description 查询我的优惠券
     * @Date 2024/6/14
     * @Param [userId]
     */
    @Override
    public Map<String,Object> selectMyQrCode(String openId) {
        Map<String,Object> resMap = new HashMap<>();

        QueryWrapper<MobileUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(MobileUser::getOpenId,openId);
        MobileUser user = userMapper.selectOne(userQueryWrapper);
        if(null == user){
            throw new MessageException("未查询到合法用户");
        }
        QueryWrapper<MobileUserCoupon> couponQueryWrapper = new QueryWrapper<>();
        couponQueryWrapper.lambda().eq(MobileUserCoupon::getUserId,user.getId());
        MobileUserCoupon coupon = couponMapper.selectOne(couponQueryWrapper);
        if(null == coupon){
            resMap.put("status","-1");
            resMap.put("qrCodeImg","");
        }else {
            String qrCodeBase64 = QRCodeUtil.createCodeToOutputStream(coupon.getCouponQrCode());
            resMap.put("qrCodeImg",qrCodeBase64);
            resMap.put("status",coupon.getStatus());
        }
        return resMap;
    }

    /**
     * @param param
     * @return void
     * @Author liumaotong
     * @Description 核销
     * @Date 2024/6/14
     * @Param [qrcode]
     */
    @Override
    public void costCoupon(MobileUserCoupon param) {
        QueryWrapper<MobileUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MobileUser::getPhone,param.getPhoneNumber());
        MobileUser user = userMapper.selectOne(queryWrapper);
        if(null == user){
            throw new MessageException("未查询到合法用户");
        }
        QueryWrapper<MobileUserCoupon> couponQueryWrapper = new QueryWrapper<>();
        couponQueryWrapper.lambda().eq(MobileUserCoupon::getUserId,user.getId());
        couponQueryWrapper.lambda().eq(MobileUserCoupon::getCouponQrCode,param.getCouponQrCode());
        MobileUserCoupon coupon = couponMapper.selectOne(couponQueryWrapper);
        if(null == coupon){
            throw new MessageException("核销失败，未查询到优惠券数据");
        }
        MobileUserCoupon userCoupon = new MobileUserCoupon();
        userCoupon.setStatus("1");
        userCoupon.setId(coupon.getId());
        couponMapper.updateById(userCoupon);
    }
}